package vn.vnvas.xoso;

import java.util.ArrayList;

import vn.vnvas.xoso.User.Register;
import vn.vnvas.xoso.User.Setting;
import vn.vnvas.xoso.mienbac.KetQuaTheoTinh;
import vn.vnvas.xoso.soicau.SoiCau;
import vn.vnvas.xoso.trungnam.miennam;
import vn.vnvas.xoso.trungnam.mientrung;
import vn.vnvas.xoso.utils.SmartDialog;
import vn.vnvas.xoso.utils.Webservice_User;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Watson.OnOptionsItemSelectedListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;

public class XoSoVN extends SherlockActivity implements
		OnOptionsItemSelectedListener {

	private ListView mlistView;
	private ArrayList<Mien> array;
	private myAdapter adapter;
	private SharedPreferences preferences;
	private SharedPreferences.Editor editor;
	private Webservice_User mWebservice_User;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mlistView = (ListView) findViewById(R.id.listViewMain);
		array = new ArrayList<Mien>();
		adapter = new myAdapter(this, R.layout.layout_item, array);
		mlistView.setAdapter(adapter);

		Mien mienbac = new Mien("Miền Bắc", BitmapFactory.decodeResource(
				getResources(), R.drawable.bac), "");
		array.add(mienbac);

		Mien mientrung = new Mien("Miền Trung", BitmapFactory.decodeResource(
				getResources(), R.drawable.trung), "");
		array.add(mientrung);

		Mien miennam = new Mien("Miền Nam", BitmapFactory.decodeResource(
				getResources(), R.drawable.nam), "");
		array.add(miennam);

		Mien thongtin = new Mien("Soi Cầu", BitmapFactory.decodeResource(
				getResources(), R.drawable.info), "");
		array.add(thongtin);

		mlistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long id) {
				// String mien = ((LayoutItem)view).mien.getText().toString();
				switch (position) {
				case 0:
					Intent mienbac = new Intent(getBaseContext(),
							KetQuaTheoTinh.class);
					startActivity(mienbac);
					break;
				case 1:
					Intent mientrung = new Intent(getBaseContext(),
							mientrung.class);
					startActivity(mientrung);
					break;
				case 2:
					// Toast.makeText(getBaseContext(), "Da chon: "+mien,
					// Toast.LENGTH_LONG).show();
					Intent miennam = new Intent(getBaseContext(), miennam.class);
					startActivity(miennam);
					break;
				case 3:
					// Toast.makeText(getBaseContext(), "Da chon: "+mien,
					// Toast.LENGTH_LONG).show();
					String strUsername = preferences.getString("username", "");
					String strPassword = preferences.getString("password", "");
					if (strUsername.equals("") || strPassword.equals("")) {
						SmartDialog
								.SmartDialogOneButton(
										XoSoVN.this,
										"Error",
										"Vui lòng đăng nhập để sử dụng chức năng soi cầu!",
										"Ok");
					} else {
						String loginRespond = mWebservice_User.WsLogin("Login",
								"username", strUsername, "password",
								strPassword);
						if (loginRespond.equals("\"Success\"")) {
							startActivity(new Intent(getBaseContext(),
									SoiCau.class));
						} else {
							SmartDialog
									.SmartDialogOneButton(
											XoSoVN.this,
											"Error",
											"Vui lòng nhắn tin gia hạn để tiếp tục sử dụng dịch vụ!",
											"Ok");
						}
					}

					break;

				default:
					break;
				}

			}
		});
		preferences = PreferenceManager.getDefaultSharedPreferences(this);
		editor = preferences.edit();
		mWebservice_User = new Webservice_User();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater mi = getSupportMenuInflater();
		mi.inflate(R.menu.activity_main, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(
			com.actionbarsherlock.view.MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case R.id.Login:
			ShowDialog();
			break;
		case R.id.Setting:
			String strUsername1 = preferences.getString("username", "");
			String strPassword1 = preferences.getString("password", "");
			if (strUsername1.equals("") || strPassword1.equals("")) {
				SmartDialog.SmartDialogOneButton(XoSoVN.this, "Error",
						"Vui lòng đăng nhập!", "Ok");
			} else {
				startActivity(new Intent(getBaseContext(), Setting.class));
			}
			break;
		case R.id.Register:
			startActivity(new Intent(getBaseContext(), Register.class));
			break;
		case R.id.Logout:
			editor.putString("username", "");
			editor.putString("password", "");
			editor.commit();
			SmartDialog.SmartDialogOneButton(XoSoVN.this, "Success",
					"Đăng xuất thành công!", "Ok");
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void ShowDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(XoSoVN.this);
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.dialog_signin, null);
		builder.setTitle("Signup");
		builder.setView(v);
		builder.setPositiveButton("Login",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						AlertDialog mAlertDialog = (AlertDialog) arg0;
						EditText edtUsername = (EditText) mAlertDialog
								.findViewById(R.id.username);
						EditText edtPassword = (EditText) mAlertDialog
								.findViewById(R.id.password);
						String login = mWebservice_User.WsLogin("Login",
								"username", edtUsername.getText().toString(),
								"password", edtPassword.getText().toString());
						if (login.equals("\"Fail\"")) {
							SmartDialog.SmartDialogOneButton(XoSoVN.this,
									"Error", "Sai Username hoặc Passwork!",
									"Ok");
						} else if (login.equals("\"Success\"")) {
							SmartDialog.SmartDialogOneButton(XoSoVN.this,
									"Success", "Đăng nhập thành công!", "Ok");
							editor.putString("username", edtUsername.getText()
									.toString());
							editor.putString("password", edtPassword.getText()
									.toString());
							editor.commit();
						}

					}
				});
		builder.setNegativeButton("Register",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub

					}
				});
		builder.show();
	}

}